package com.swufe.catchme;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class Score extends AppCompatActivity  {
    private static final String TAG = "Score";
    private int logScore;
    private int newscore;
    TextView nowscore;
    TextView oldscore;
    Button back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);

        nowscore = findViewById(R.id.nowscore);
        oldscore = findViewById(R.id.oldscore);
        back = findViewById(R.id.returnmain);

        Intent intent = getIntent();
        newscore = intent.getIntExtra("score", 0);

        SharedPreferences sharedPreferences = getSharedPreferences("myscore", Activity.MODE_PRIVATE);
        logScore = sharedPreferences.getInt("last_score",0);
        Log.i("List", "lastScore=" + logScore);
        if(newscore<logScore){
            nowscore.setText(String.valueOf(newscore));
            oldscore.setText(String.valueOf(logScore));
        }else{
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putInt("last_score",newscore);
            editor.commit();
            Log.i(TAG, "onActivityResult: 得分更新到sharedPreferences");
            nowscore.setText(String.valueOf(newscore));
            oldscore.setText(String.valueOf(newscore));
        }

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent Back = new Intent(Score.this, MainActivity.class);
                startActivity(Back);
                finish();
            }
        });
    }


}